﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Common.View.Controls
{
    /// <summary>
    /// RangeTextBox.xaml 的交互逻辑
    /// </summary>
    public partial class RangeTextBox : UserControl,INotifyPropertyChanged
    {
        public static readonly DependencyProperty TitleProperty =
            DependencyProperty.Register("Title", typeof(string), typeof(RangeTextBox), new PropertyMetadata(string.Empty));
        public static readonly DependencyProperty UnitProperty =
            DependencyProperty.Register("Unit", typeof(string), typeof(RangeTextBox), new PropertyMetadata(string.Empty));
        public static readonly DependencyProperty NecessaryProperty =
            DependencyProperty.Register("Necessary", typeof(bool), typeof(RangeTextBox), new PropertyMetadata(false));
        public static readonly DependencyProperty MinValueProperty =
            DependencyProperty.Register("MinValue", typeof(object), typeof(RangeTextBox), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnPropertyChanged));
        public static readonly DependencyProperty MaxValueProperty =
            DependencyProperty.Register("MaxValue", typeof(object), typeof(RangeTextBox), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnPropertyChanged));
        public string Title
        {
            get { return (string)GetValue(TitleProperty); }
            set { SetValue(TitleProperty, value); }
        }
        public string Unit
        {
            get { return (string)GetValue(UnitProperty); }
            set { SetValue(UnitProperty, value); }
        }
        public string MinValue
        {
            get { return (string)GetValue(MinValueProperty); }
            set { SetValue(MinValueProperty, value); }
        }
        public string MaxValue
        {
            get { return (string)GetValue(MaxValueProperty); }
            set { SetValue(MaxValueProperty, value); }
        }
        public bool Necessary
        {
            get { return (bool)GetValue(UnitProperty); }
            set { SetValue(UnitProperty, value); }
        }
        public event PropertyChangedEventHandler PropertyChanged;
        private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = d as RangeTextBox;
            control?.OnPropertyChanged(e.Property.Name);
        }
        protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
        public RangeTextBox()
        {
            InitializeComponent();
        }
        private void NumericTextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            TextBox textBox = sender as TextBox;
            if (textBox != null)
            {
                bool handle = e.Text.Length == 1 && !char.IsDigit(e.Text, 0) && e.Text != "." && e.Text != "-"; // Allow negative numbers and decimals
                if (textBox.Text.Contains(".") && e.Text == ".")
                {
                    handle = true;
                }
                if (e.Text == "-" && !string.IsNullOrEmpty(textBox.Text))
                {
                    handle = true;
                }
                if (e.Text.Length != 1)
                {
                    handle = true;
                }
                e.Handled = handle;
            }
        }
    }
}
